local skel = fk.CreateSkill {
  name = "sj_chongzouxiyou__shenbisanshe",
}

Fk:loadTranslationTable{
  ["sj_chongzouxiyou__shenbisanshe"] = "神避三舍",
  [":sj_chongzouxiyou__shenbisanshe"] = "回合开始时，你可以展示【杀】或普通锦囊牌，令攻击范围内的任意角色选择是否弃置一张符合的牌，如此做的角色本回合视为在攻击范围外，你本回合使用展示牌时目标改为未如此做的角色。",

  ["#sj_chongzouxiyou__shenbisanshe"] = "神避三舍：你可以展示【杀】或普通锦囊牌，令攻击范围内的任意角色选择是否弃置一张符合的牌",
  ["#sj_chongzouxiyou__shenbisanshe-dis1"] = "神避三舍：你可以弃置一张【杀】，令你本回合在 %src 的攻击范围外",
  ["#sj_chongzouxiyou__shenbisanshe-dis2"] = "神避三舍：你可以弃置一张普通锦囊牌，令你本回合在 %src 的攻击范围外",

  ["@@sj_chongzouxiyou__shenbisanshe-turn"] = "神避三舍",
  ["@@sj_chongzouxiyou__nifoguohe-turn"] = "泥佛过河",

  ["$sj_chongzouxiyou__shenbisanshe1"] = "",
  ["$sj_chongzouxiyou__shenbisanshe2"] = "",
}


skel:addEffect(fk.TurnStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and not player:isKongcheng()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = table.filter(room:getOtherPlayers(player),function (t)
      return player:inMyAttackRange(t)
    end)
    if #tos > 0 then
      local ids = table.filter(player:getCardIds("h"),function (id)
        local card = Fk:getCardById(id)
        return card.trueName == "slash" or card:isCommonTrick()
      end)
      local tos,cards,success = room:askToChooseCardsAndPlayers(player,{
        targets = tos,
        min_num = 1,
        max_num = #tos,
        min_card_num = 1,
        max_card_num = 1,
        pattern = tostring(Exppattern{id = ids}),
        cancelable = true,
        prompt = "#sj_chongzouxiyou__shenbisanshe",
        skill_name = skel.name,
      })
      if success and #tos > 0 and #cards > 0 then
        event:setCostData(self,{tos = tos, cards = cards})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local showcard = Fk:getCardById(cost_data.cards[1])
    player:showCards(showcard)
    room:setCardMark(showcard,"@@sj_chongzouxiyou__shenbisanshe-turn",1)
    local slash = showcard.trueName == "slash"
    local tos = cost_data.tos
    room:sortByAction(tos)
    for _, to in ipairs(tos) do
      if not to.dead then
        local pattern = slash and "slash" or ".|.|.|.|.|^(basic,equip,delayed_trick)"
        if #room:askToDiscard(to,{
          min_num = 1,
          max_num = 1,
          skill_name = skel.name,
          cancelable = true,
          pattern = pattern,
          prompt = slash and "#sj_chongzouxiyou__shenbisanshe-dis1:"..player.id or "#sj_chongzouxiyou__shenbisanshe-dis2:"..player.id,
        }) > 0 then
          room:setPlayerMark(to,"@@sj_chongzouxiyou__shenbisanshe-turn",1)
        else
          room:setPlayerMark(to,"@@sj_chongzouxiyou__nifoguohe-turn",1)
        end
      end
    end
  end,
})

skel:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.card:getMark("@@sj_chongzouxiyou__shenbisanshe-turn") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.tos = table.filter(player.room.alive_players,function (t)
      return t:getMark("@@sj_chongzouxiyou__nifoguohe-turn") > 0
    end)
  end,
})


skel:addEffect("atkrange", {
  without_func = function (self, from, to)
    return from:hasSkill(skel.name,true) and to:getMark("@@sj_chongzouxiyou__shenbisanshe-turn") > 0
  end,
})

return skel
